package com.ebt.m.commons.widgets.view;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.commons.R;
import com.ebt.m.commons.widgets.BaseRxDialogFragment;


/**
 * Created by wanglu on 2017/6/21.
 */

public class EBTDialog extends BaseRxDialogFragment implements View.OnClickListener {

    TextView dialogTitle;
    ImageButton dialogClose;
    TextView dialogMessage;
    FrameLayout contentContainer;
    Button btn1;
    View bottomDivider;
    Button btn2;
    LinearLayout bottomContainer;

    private String message;
    private String title;
    private View content;
    private boolean closeEnable = false;

    private String singleButtonText;
    private String leftButtonText;
    private String rightButtonText;
    private View.OnClickListener singleButtonListener;
    private View.OnClickListener leftButtonListener;
    private View.OnClickListener rightButtonListener;

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCanceledOnTouchOutside(false);
        View view = inflater.inflate(R.layout.layout_base_dialog, null);

        dialogTitle = view.findViewById(R.id.dialog_title);
        dialogClose = view.findViewById(R.id.dialog_close);
        dialogMessage = view.findViewById(R.id.dialog_message);
        contentContainer = view.findViewById(R.id.content_container);
        btn1 = view.findViewById(R.id.btn1);
        bottomDivider = view.findViewById(R.id.bottom_divider);
        btn2 = view.findViewById(R.id.btn2);
        bottomContainer = view.findViewById(R.id.bottom_container);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        dialogClose.setOnClickListener(this);
        loadView();
        return view;
    }

    private void loadView() {
        //title
        if (!TextUtils.isEmpty(title)) {
            dialogTitle.setVisibility(View.VISIBLE);
            dialogTitle.setText(title);
            dialogClose.setVisibility(View.GONE);
        } else {
            dialogTitle.setVisibility(View.GONE);
        }
        //message
        if (!TextUtils.isEmpty(message)) {
            dialogMessage.setVisibility(View.VISIBLE);
            dialogMessage.setText(message);
        } else {
            dialogMessage.setVisibility(View.GONE);
        }

        //content
        if (content != null) {
            dialogMessage.setVisibility(View.GONE);
            contentContainer.setVisibility(View.VISIBLE);
            contentContainer.removeAllViews();
            if (content.getParent() != null)
                ((ViewGroup) content.getParent()).removeAllViews();
            contentContainer.addView(content);
        } else {
            contentContainer.setVisibility(View.GONE);
        }

        //close
        if (closeEnable) {
            dialogClose.setVisibility(View.VISIBLE);
        } else {
            dialogClose.setVisibility(View.GONE);
        }

        //button
        if (TextUtils.isEmpty(singleButtonText) && TextUtils.isEmpty(leftButtonText)
                && TextUtils.isEmpty(rightButtonText)) {
            bottomContainer.setVisibility(View.GONE);
        } else {
            bottomContainer.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(singleButtonText) && singleButtonListener != null) {
            btn1.setText(singleButtonText);
            btn1.setVisibility(View.VISIBLE);
            bottomDivider.setVisibility(View.GONE);
            btn2.setVisibility(View.GONE);
            btn1.setTextColor(getResources().getColor(R.color.common_bg_bar));
        } else {
            if (!TextUtils.isEmpty(leftButtonText) && leftButtonListener != null) {
                btn1.setText(leftButtonText);
                btn1.setVisibility(View.VISIBLE);
                btn1.setTextColor(Color.parseColor("#333333"));
            }
            if (!TextUtils.isEmpty(rightButtonText) && rightButtonListener != null) {
                btn2.setText(rightButtonText);
                btn2.setVisibility(View.VISIBLE);
                bottomDivider.setVisibility(View.VISIBLE);
            } else {
                btn2.setVisibility(View.GONE);
                bottomDivider.setVisibility(View.GONE);
            }
        }
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setCloseEnable(boolean enable) {
        this.closeEnable = enable;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setContent(View view) {
        this.content = view;
    }

    public void setSingleButton(String buttonText, View.OnClickListener listener) {
        if (!TextUtils.isEmpty(buttonText) && listener != null) {
            this.singleButtonText = buttonText;
            this.singleButtonListener = listener;
        }
    }

    public void setLeftButton(String buttonText, View.OnClickListener listener) {
        if (!TextUtils.isEmpty(buttonText) && listener != null) {
            this.leftButtonText = buttonText;
            this.leftButtonListener = listener;
        }
    }

    public void setRightButton(String buttonText, View.OnClickListener listener) {
        if (!TextUtils.isEmpty(buttonText) && listener != null) {
            this.rightButtonText = buttonText;
            this.rightButtonListener = listener;
        }
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.dialog_close) {
            dismiss();
        } else if (id == R.id.btn1) {
            dismiss();
            if (singleButtonListener != null) {
                singleButtonListener.onClick(view);
            } else if (leftButtonListener != null) {
                leftButtonListener.onClick(view);
            }
        } else if (id == R.id.btn2) {
            dismiss();
            if (rightButtonListener != null)
                rightButtonListener.onClick(view);

        }

    }
}
